package com.fish.yssb.ui.page.activity.other;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.fish.yssb.Consts;
import com.fish.yssb.R;
import com.fish.yssb.api.retData;
import com.fish.yssb.api.setApi;
import com.fish.yssb.ui.page.activity.base.BaseActivity;
import com.fish.yssb.ui.page.util.TopToastUtil;
import com.hy.srv101.Dim2Data;
import com.hy.srv101.DimXLib;
import com.hy.srv101.IDimFace;
import com.jaeger.library.StatusBarUtil;
import com.syd.oden.circleprogressdialog.core.CircleProgressDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * A login screen that offers login via email/password.
 */
public class ReadMQActivity extends BaseActivity implements OnClickListener {
    private static final String TAG = ReadMQActivity.class.getSimpleName();

    private TextView btn_1,btn_2;
    private LinearLayout back;
    private TimePickerView pvTime;
    private LinearLayout ll_content,ll_chantime,btn_read;
    private ImageView ck_curtime,iv_nodata;
    private TextView tv_time;
    private TextView tv_content1,tv_content2,tv_content3,tv_content4,tv_content5,tv_content6,tv_content7;
    private CircleProgressDialog circleProgressDialog
            ;

    public static void start(Context context) {
        Intent intent = new Intent(context, ReadMQActivity.class);
        context.startActivity(intent);
    }

    @Override
    public  void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setDarkMode(this);
        setContentView(R.layout.activity_readmq);
        initview();
        circleProgressDialog = new CircleProgressDialog(ReadMQActivity.this);
        circleProgressDialog.setText("命令执行中");
    }


    private boolean flag=true;
    //初始化视图
    @SuppressLint("WrongViewCast")
    private void initview() {
        //登录层、下拉层、其它登录方式层

        back = findViewById(R.id.tv_back);
        back.setOnClickListener(this);

        tv_content1 = findViewById(R.id.tv_content1);
        tv_content2 = findViewById(R.id.tv_content2);
        tv_content3 = findViewById(R.id.tv_content3);
        tv_content4 = findViewById(R.id.tv_content4);
        tv_content5 = findViewById(R.id.tv_content5);
        tv_content6 = findViewById(R.id.tv_content6);
        tv_content7 = findViewById(R.id.tv_content7);

        iv_nodata = findViewById(R.id.iv_nodata);
        btn_read = findViewById(R.id.btn_read);
        ll_content = findViewById(R.id.ll_content);
        btn_read.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                String strSq = "";
                if(Consts.isRealSQ) {
                    Dim2Data dimData = new Dim2Data(IDimFace.Dim_Two, 256);

                    DimXLib.StartScan(dimData);

                    if (dimData.GetDim2DataLen() > 0) {//获取到有效的扫码数据
                        byte[] data = dimData.GetDim2Data();//获取扫码数据

                        try {
                            strSq = new String(data, "UTF-8");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }

                    }
                }else {
                    String mstrRestartSend = "D6 C7 C4 DC C5 E4 B1 E4 D6 D5 B6 CB B1 EA D7 BC " +
                            "D0 CD A3 AC C0 E0 D0 CD A3 BA 54 32 30 A3 AC B3 " +
                            "A7 C9 CC A3 BA B3 A4 D4 B0 C9 EE C8 F0 BC CC B1 " +
                            "A3 D7 D4 B6 AF BB AF D3 D0 CF DE B9 AB CB BE A3 " +
                            "AC D0 CD BA C5 A3 BA 50 52 53 2D 33 33 38 31 A3 " +
                            "AC 49 44 A3 BA 54 32 30 30 45 30 45 53 52 30 36 " +
                            "30 32 30 31 39 31 32 32 34 30 32 30 31 A3 AC D3 " +
                            "B2 BC FE B0 E6 B1 BE A3 BA 48 56 30 31 2E 30 30 " +
                            "A3 AC C9 FA B2 FA C8 D5 C6 DA A3 BA 32 30 31 39 " +
                            "C4 EA 31 32 D4 C2 32 34 C8 D5";
                    byte[] mRestart = null;
                    mRestart = HexCommandtoByte(mstrRestartSend.getBytes());
                    try {
                        strSq = new String(mRestart, "GB2312");
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }

                    ll_content.setVisibility(View.VISIBLE);
                    iv_nodata.setVisibility(View.GONE);
                    String str[] = strSq.split("，");
                    if(str.length>0)
                    {
                        tv_content1.setText(str[0]);
                    }
                    if(str.length>1)
                    {
                        tv_content2.setText(str[1]);
                    }
                    if(str.length>2)
                    {
                        tv_content3.setText(str[2]);
                    }
                    if(str.length>3)
                    {
                        tv_content4.setText(str[3]);
                    }
                    if(str.length>4)
                    {
                        tv_content5.setText(str[4]);
                    }
                    if(str.length>5)
                    {
                        tv_content6.setText(str[5]);
                    }
                    if(str.length>6)
                    {
                        tv_content7.setText(str[6]);
                    }





            }
        });


    }

    public static byte[] HexCommandtoByte(byte[] data) {
        if (data == null) {
            return null;
        }
        int nLength = data.length;

        String strTemString = new String(data, 0, nLength);
        String[] strings = strTemString.split(" ");
        nLength = strings.length;
        data = new byte[nLength];
        for (int i = 0; i < nLength; i++) {
            if (strings[i].length() != 2) {
                data[i] = 00;
                continue;
            }
            try {
                data[i] = (byte)Integer.parseInt(strings[i], 16);
            } catch (Exception e) {
                data[i] = 00;
                continue;
            }
        }

        return data;
    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.tv_back:
                finish();
                break;

            default:
                break;
        }


    }



}

